<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function PriorityQueue() {
            // js内部类
            function QueueElement(element, priority) {
                this.element = element;
                this.priority = priority;
            }
            this.items = [];
            // 添加队列元素
            PriorityQueue.prototype.enqueue = function (element, priority) {
                var queueElement = new QueueElement(element, priority);

                if (this.items.length == 0) {
                    this.items.push(queueElement)
                } else {
                    var isAdd = false;
                    for (var i = 0; i < this.items.length; i++) {
                        if (this.items[i].priority > queueElement.priority) {
                            this.items.splice(i, 0, queueElement);
                            isAdd = true;
                            break;
                        }
                    }
                    if (!isAdd) {
                        this.items.push(queueElement)
                    }
                }
            }
            // 移除队列的第一（即排在队列最前面的）项，并返回被移除的元素；
            PriorityQueue.prototype.dequeue = function () {
                this.items.shift(this.items[0])
            }
            // 返回队列中的第一个元素
            PriorityQueue.prototype.front = function () {
                return this.items[0]
            }
            // 为空？
            PriorityQueue.prototype.isEmpty = function () {
                return this.items.length === 0 ? true : false
            }
            PriorityQueue.prototype.size = function () {
                return this.items.length;
            }
            PriorityQueue.prototype.toString = function () {
                var str = '';
                for (var i = 0; i < this.items.length; i++) {
                    str += this.items[i].element + '-' + this.items[i].priority + ' '
                }
                return str;
            }
        }

        var priorityQueue = new PriorityQueue();
        priorityQueue.enqueue('a', 900);
        priorityQueue.enqueue('b', 100);
        priorityQueue.enqueue('c', 400);
        priorityQueue.enqueue('d', 300);
        priorityQueue.enqueue('e', 200);
        console.log(priorityQueue.toString()); 
 
    //b-100 e-200 d-300 c-400 a-900 
    </script>
</body>

</html>